<template>
    <div style="display:flex;flex-direction:column;width:100%;margin-top:25px">
        
        <div style="margin-bottom:10px;background:#eee;line-height:40px;padding-left:5px">基本信息</div>
        <p-form-model :labelCol="{ span: 6 }" :wrapperCol="{ span: 18   }"
            ref="ruleForm"
            :model="form"
            :rules="rules"
            style="margin-top:20px">
            <p-row :guter="20">
                <p-col :span="12">
                    <p-form-model-item ref="number" label="单据编号" prop="number">
                        <p-input v-model="form.number" disabled />
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="source" label="来源单号" prop="source">
                        <p-input v-model="form.source"  :disabled="listData.isNewAdd" />
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="producer" label="制单人" prop="producer">
                        <p-input-search
                        class="hideName"
                        :disabled="listData.isNewAdd"
                        v-model="form.producer.name"
                        @search="onSearchDepUser('producer')">
                        <p-button slot="enterButton" :disabled="disabled">选择用户</p-button>
                        </p-input-search>
                    </p-form-model-item>
                    </p-form-model-item>

                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="date" label="制单日期" prop="date">
                        <p-date-picker v-model="defauvalue" format="YYYY-MM-DD"   @change="onChange" :disabled="listData.isNewAdd"/>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="Company" label="更改单位" prop="Company">
                        <p-select  ref="Company"  @change="handleChange" v-model="form.Company" :disabled="listData.isNewAdd">
                            <p-select-option :value="item.value" v-for="item in Company" :key="item.value">{{item.text}}</p-select-option>
                        </p-select>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="type" label="更改类型" prop="type">
                        <p-select  v-model="form.type.text" disabled>
                            <p-select-option
                            @click="ValueChange(item)"
                                :value="item.value"
                                v-for="item in processChange"
                                :key="item.value"
                                >{{ item.text }}</p-select-option
                            >
                        </p-select>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="explain" label="更改来源说明" prop="explain" >
                        <p-input v-model="form.explain"  :disabled="listData.isNewAdd"/>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="Complete" label="更改物料齐套" prop="Complete">
                        <p-switch checked-children="是" un-checked-children="否" default-checked  v-model="form.Complete" :disabled="listData.isNewAdd"/>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="beforeNumber" label="更改前产品编码" prop="beforeNumber" >
                        <p-input 
                            placeholder="请点击选择"
                            v-model="form.beforeNumber"
                            readonly
                            @change="nulberListVisibleOk"
                            ref="userNameInput"
                            @click="openPop('before')"
                           :disabled="listData.isNewAdd"
                            >
                            <p-icon slot="prefix" type="cluster" />
                        </p-input>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="beforeName" label="更改前产品名称" prop="beforeName">
                        <p-input v-model="form.beforeName" :disabled="listData.isNewAdd"/>
                    </p-form-model-item>
                </p-col>
                 <p-col :span="12">
                    <p-form-model-item ref="afterNumber" label="更改后产品编码" prop="afterNumber">
                        <p-input 
                            placeholder="请点击选择"
                            v-model="form.afterNumber"
                            @change="nulberListVisibleOk"
                            @click="openPop('after')"
                           :disabled="listData.isNewAdd || listData.isTypeA" 
                            >
                            <p-icon slot="prefix" type="cluster" />
                        </p-input>
                    </p-form-model-item>
                </p-col>
                <p-col :span="12">
                    <p-form-model-item ref="aftername" label="更改后产品名称" prop="afterName">
                        <p-input v-model="form.afterName"  :disabled="listData.isNewAdd || listData.isTypeA" />
                    </p-form-model-item>
                </p-col>
                 <p-col :span="12">
                    <p-form-model-item ref="planName" label="计划处理人" prop="planName">
                        <p-input-search
                        class="hideName"
                        :disabled="listData.isNewAdd"
                        v-model="form.planName"
                        @search="onSearchDepUser('planName')">
                        <p-button slot="enterButton" :disabled="disabled">选择用户</p-button>
                        </p-input-search>
                    </p-form-model-item>
                 
                </p-col>
                 <p-col :span="12">
                    <p-form-model-item ref="Craftsman" label="工艺处理人" prop="CraftsmanName">
                        <p-input-search
                        class="hideName"
                        :disabled="listData.isNewAdd"
                        v-model="form.CraftsmanName"
                        @search="onSearchDepUser('Craftsman')">
                        <p-button slot="enterButton" :disabled="disabled">选择用户</p-button>
                        </p-input-search>
                    </p-form-model-item>
                </p-col>
                  <p-col :span="12">
                    <p-form-model-item ref="main" label="工艺主管" prop="mainName">
                        <p-input-search
                        class="hideName"
                        :disabled="listData.isNewAdd"
                        v-model="form.mainName"
                        @search="onSearchDepUser('main')">
                        <p-button slot="enterButton" :disabled="disabled">选择用户</p-button>
                        </p-input-search>
                    </p-form-model-item>
                </p-col>
                  <p-col :span="12">
                    <p-form-model-item ref="manager" label="工艺经理" prop="managerName">
                        <p-input-search
                        class="hideName"
                        :disabled="listData.isNewAdd"
                        v-hide="changemanager"
                        v-model="form.managerName"
                        @search="onSearchDepUser('manager')"> 
                        <p-button slot="enterButton" v-show="changemanager" :disabled="disabled">选择用户</p-button>
                        </p-input-search>
                    </p-form-model-item>
                </p-col>
                 <p-col :span="24">
                    <p-form-model-item ref="Material " label="包材保留" prop="Material " :labelCol={span:3}>
                        <p-select
                            mode="multiple"
                            style="width: 100%"
                            @change="handleChange"
                            v-model="form.Material" 
                            :disabled="listData.isNewAdd"
                        >
                            <p-select-option v-for="item in InventoryPackaging" :key="item.value" :value="item.value">
                            {{ item.text}}
                            </p-select-option>
                        </p-select>
                    </p-form-model-item>
                </p-col>
                <p-col :span="24">
                    <p-form-model-item  label="产品更改内容" :labelCol={span:3}>
                        <p-select
                            mode="multiple"
                            style="width: 100%"
                            @change="handleChange"
                            v-model="form.list" 
                            :disabled="listData.isNewAdd"
                        >
                            <p-select-option :value="item.value" name="type" v-for="item in list" :key="item.value">{{item.text}}
                            </p-select-option>
                        </p-select>
                    </p-form-model-item>
                </p-col>
                 <p-col :span="24">
                    <p-form-model-item  label="工艺路线" :labelCol={span:3}>
                        <a href="javascript:;" @click="handletemporary">临时BOM物料分配表</a>
                    </p-form-model-item>
                </p-col>
                <p-col :span="24">
                    <p-form-model-item  label="产品更改范围" :labelCol={span:3}>
                       <p-div>
                        <a href="javascript:;" @click="lockdetails">产品锁定单号明细</a>
                        <!-- <a style="margin-left:40px" href="javascript:;" @click="lockdetails">产品更改明细</a> -->
                       </p-div>
                    </p-form-model-item>
                </p-col>
                
               
            </p-row>
        </p-form-model>
        <PageTitle>工艺路线编排</PageTitle>
        <p-skeleton :loading="!showFlag" active />
        <drawRoute :pdata="pdata" v-if="showFlag" ref="DrawRoute"/>
        <div v-show="isStatus">
          <div class="table-operator" style="text-align:right;margin-top:20px" v-show="this.listData.Approval">
            <p-button @click="saveButton" type="primary" :loading="btnLoading">保存</p-button>
            <p-button @click="submitButton" type="primary" :loading="btnLoading">提交</p-button>
        </div>
        <div class="table-operator" style="text-align:right;margin-top:20px" v-show="!this.listData.Approval">
            <p-button @click="confirm" type="primary" :loading="btnLoading">确认</p-button>
            <p-button @click="reject" type="primary" :loading="btnLoading">驳回</p-button>
        </div>
        </div>
       <!-- 临时BOM物料分配表 -->
      <p-modal destroyOnClose="true" v-model="temporaryVisible" title @ok="temporaryOk" width="70%">
        <temporary :before="form.before"></temporary>
      </p-modal>
      <!-- 产品锁定明细 -->
      <p-modal destroyOnClose="true" v-model="lockdetailsVisible" title @ok="lockdetailsOk" width="70%">
        <lockdetails></lockdetails>
      </p-modal>
      <!-- 产品编码列表 -->
         <p-modal destroyOnClose="true" v-model="nulberListVisible" title @ok="nulberListVisibleOk" width="70%" >
        <nulberListVisible  @getNumber="getlist"></nulberListVisible>
      </p-modal>
        <!-- 人员选择模块 -->
      <j-select-user-by-dep-modal
      ref="selectModal"
      :modal-width="1250"
      :multi="true"
      @ok="selectOK"
      />

    </div>
</template>
<script>
import Vue from "vue";
import mesPeProcess from "@/api/mesPeProcess";
import PageTitle from "@/components/PageTitle";
import drawRoute from "./handlePocess/drawRoute";
import { ajaxGetDictItems } from "@/api/api";
import {
  postAction,
  getAction,
  deleteAction,
  putAction,
  api,
} from "@/api/manage";
import temporary from "./temporary";
import lockdetails from "./Lockdetails";
import JSelectUserByDepModal from "./JSelectUserByDepModal";
import nulberListVisible from "./nulberListVisible";
import moment from "moment";
import essentialReplaceVue from "../../datacollect/collect/essential-replace.vue";
export default {
  inject: ["userInfo"],
  components: {
    PageTitle,
    drawRoute,
    temporary,
    lockdetails,
    JSelectUserByDepModal,
    nulberListVisible,
  },
  props: {
    listData: {
      type: Object,
      required: false,
    },
  },
  data() {
    return {
      moment,
      peo: "",

      list: [], //产品内容更改
      InventoryPackaging: [], //包材保留
      processChange: [], //更改类型
      Company: [],
      btnLoading: false,
      temporaryVisible: false,
      lockdetailsVisible: false,
      nulberListVisible: false,
      changemanager: true,
      temporary: [],
      routerCode: "",
      version: "",
      pdata: "",
      changeNumber: "",
      defauvalue: moment(new Date(), "YYYY-MM-DD"),
      showFlag: false,
      isStatus:true,
      idGetInfo: {},
      form: {
        plan: {},
        planName: "",
        type: {},
        producer: {},
        Craftsman: {},
        before: {},
        main: {},
        manager: {},
        CraftsmanName:'',
        managerName:'',
        mainName:'',
        beforeNumber: "",
        beforeName: "",
        afterName: "",
        afterNumber: "",
        mainName: "",
        managerName: "",
        Complete: false,
      },
      rules: {
        source: [{ required: true, message: "请填写来源单号" }],
        producer: [{ required: true, message: "请填写制单人" }],
        Company: [{ required: true, message: "请填写更改单位" }],
        type: [{ required: true, message: "请选择更改类型" }],
        explain: [{ required: true, message: "请填写更改来源说明" }],
        beforeNumber: [{ required: true, message: "请填写更改前产品编码" }],
        beforeName: [{ required: true, message: "请填写更改前产品名称" }],
        afterNumber: [
          { required: !this.listData.isTypeA, message: "请填写更改后产品编码" },
        ],
        afterName: [
          { required: !this.listData.isTypeA, message: "请填写更改后产品名称" },
        ],
        CraftsmanName: [{ required: true, message: "请填写工艺处理人" }],
        planName: [{ required: true, message: "请填写计划处理人" }],
        managerName: [{ required: true, message: "请填写工艺经理" }],
        mainName: [{ required: true, message: "请填写工艺主管" }],
        Material: [{ required: true, message: "请选择包材保留" }],
      },
      url: {
        save: "/mes-craft/craft/mesPeBill", //"新增返工单"
        getRouter:
          "/mes-craft/craft/mesPeProcessRoute/getRouterByProjectCodeAndRouterType", //根据产品编码获取工艺路线
      },
    };
  },
  computed: {
    handle() {
      return this.$route.query.handle;
    },
  },
  mounted() {
    
    this.lodacontent();
    this.form.type = this.listData.typeChange;
    if(this.listData.billStatus==6){this.isStatus=false}
    this.loadForm();
  },
  methods: {
    queryList() {
      if (this.listData.hand == "change") {
        let arr = this.listData.productChangeContent.split(",");
        let array = [];
        arr.forEach((item) => {
          array.push(
            this.list.filter((items) => {
              return items.value == item;
            })[0].value
          );
        });
        this.form.list = array;
      }
      if (this.listData.hand == "change") {
        let arr = this.listData.packingMaterialRetention.split(",");
        let array = [];
        arr.forEach((item) => {
          array.push(
            this.InventoryPackaging.filter((items) => {
              return items.value == item;
            })[0].value
          );
        });
        this.form.Material = array;
      }
    },
    loadForm() {
      if (this.listData.hand == "add") {
        this.getnewDate();
        this.form.producer = {
          name: this.userInfo.empname,
          uid: this.userInfo.empno,
        };
        this.ifADD();
      } else {
        this.setFormData(this.listData);
        if (this.listData.productCodeChangeAfter) {
          this.changeNumber = "after";
        } else {
          this.changeNumber = "before";
        }
        this.nulberListVisibleOk();
      }
    },
    // 根据数据赋值给表单
    setFormData(data) {
      console.log(data);
      this.form.number = data.billNum;
      this.$set(this.form, "source", data.billOriNum);
      this.$set(this.form, "explain", data.productChangeExplain);
      this.$set(this, "defauvalue", moment(data.createTime, "YYYY-MM-DD"));
      this.$set(this.form, "Company", data.changeCompany);

      this.form.producer.uid = data.billCreate;
      this.form.producer.name = data.billCreateName;
      this.form.Complete = data.changeMaterialSet == "false" ? false : true;
      this.form.beforeNumber = data.productCodeChangeBefore;
      this.form.beforeName = data.productCodeChangeBeforeName;
      this.form.afterNumber = data.productCodeChangeAfter;
      this.form.afterName = data.productCodeChangeAfterName;
      this.form.plan.uid = data.planProcessor;
      this.form.planName = data.planProcessorName;
      this.form.Craftsman.uid = data.processEmployInfos[0].code;
      this.form.CraftsmanName = data.processEmployInfos[0].name;
      this.form.main.uid = data.processSupervisor,
      this.form.mainName = data.processSupervisorName,
      this.form.manager.uid = data.processManager;
      this.form.managerName = data.processManagerName;
      if (data.productCodeChangeAfter) {
        console.log("a");
        let obj = {
          productCode: data.productCodeChangeAfter,
          routerType: 0,
        };
        this.getRout(obj);
      } else {
        console.log("b");
        let obj = {
          productCode: data.productCodeChangeBefore,
          routerType: 0,
        };
        this.getRout(obj);
      }
    },

    saveButton() {
      if (!this.listData.isNewAdd) {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            if (this.form.beforeNumber) {
              if (this.listData.hand == "add") {
                this.btnLoading = true;
                postAction(this.url.save, this.getObj(0)).then((res) => {
                  if (res.code == 0) {
                    this.$message.success("保存成功");
                    this.btnLoading = false;
                    this.$emit("closeretentionVisible");
                  } else {
                    this.$message.error(res.msg);
                  }
                });
              } else if (this.listData.hand == "change") {
                this.btnLoading = true;
                putAction(this.url.save, this.getObj(0, this.listData.id)).then(
                  (res) => {
                    if (res.code == 0) {
                      this.$message.success("修改成功");
                      this.btnLoading = false;
                      this.$emit("closeretentionVisible");
                    } else {
                      this.$message.error(res.msg);
                    }
                  }
                );
              }
            } else {
              this.$message.warning("请先选择产品编码");
            }
          }
        });
      }
    },
    submitButton() {
      if (!this.listData.isNewAdd) {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            if (this.form.beforeNumber) {
              if (this.listData.hand == "add") {
                this.btnLoading = true;
                postAction(this.url.save, this.getObj(1)).then((res) => {
                  if (res.code == 0) {
                    this.$message.success("提交成功");
                    this.btnLoading = false;
                    this.$emit("closeretentionVisible");
                  } else {
                    this.$message.error(res.msg);
                  }
                });
              } else if (this.listData.hand == "change") {
                this.btnLoading = true;
                putAction(this.url.save, this.getObj(1, this.listData.id)).then(
                  (res) => {
                    if (res.code == 0) {
                      this.$message.success("提交成功");
                      this.btnLoading = false;
                      this.$emit("closeretentionVisible");
                    } else {
                      this.$message.error(res.msg);
                    }
                  }
                );
              }
            } else {
              this.$message.warning("请先选择产品编码");
            }
          }
        });
      }
    },
    getlist(data) {
      if (this.changeNumber == "before") {
        if (this.listData.isTypeA) {
          this.form.before = { ...data[0] };
        }
        this.form.beforeNumber = data[0].mcode;
        this.form.beforeName = data[0].mname;
      } else {
        this.form.afterNumber = data[0].mcode;
        this.form.afterName = data[0].mname;
        if (!this.listData.isTypeA) {
          this.form.before = { ...data[0] };
        }
      }
    },
    //   判断新增生成单据编号
    ifADD() {
      if (this.listData.isNewAdd == false) {
        let param = {
          commonFormData: {},
          rules: [
            { ruleCode: "mes_ReturnDocument", columnName: "labelTemplateNo" },
          ],
        };
        putAction(api.ruleBaseURL, param).then((res) => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.code == 0) {
            this.$set(this.form, "number", res.data.labelTemplateNo);
          }
        });
      }
    },

    handletemporary() {
      if (this.form.before) {
        this.form.before.list = this.temporary;
        this.temporaryVisible = true;
      } else {
        this.$message.warning("请先选择产品编码");
      }
    },
    lockdetails() {
      this.lockdetailsVisible = true;
    },
    temporaryOk() {
      this.temporaryVisible = false;
    },
    lockdetailsOk() {
      this.lockdetailsVisible = false;
    },

    nulberListVisibleOk() {
      let obj;
      if (this.listData.isTypeA) {
        if (this.changeNumber == "before") {
          obj = {
            productCode: this.form.beforeNumber,
            routerType: 0,
          };
          this.getRout(obj);
        } else {
          this.nulberListVisible = false;
        }
      } else {
        if (this.changeNumber == "after") {
          console.log("b");
          obj = {
            productCode: this.form.afterNumber,
            routerType: 0,
          };
          this.getRout(obj);
        } else {
          this.nulberListVisible = false;
        }
      }
    },
    // 封装获取工艺路线函数
    // 根据产品编码获取工艺路线编码，版本
    getRout(obj) {
      this.showFlag = false;
      postAction(this.url.getRouter, obj).then((res) => {
        if (res.code == 0) {
          this.routerCode = res.data.routerCode;
          this.version = res.data.routerVersion;
          this.getRoute(this);
        } else {
          this.$message.error(res.msg);
        }
      });
      this.nulberListVisible = false;
    },
    openPop(data) {
      this.changeNumber = data;
      this.nulberListVisible = true;
    },
    onSearchDepUser(value) {
      this.$refs.selectModal.showModal();
      this.peo = value;
    },
    onChange(data, str) {
      this.form.data = str;
    },
    getnewDate() {
      this.form.data = moment(new Date()).format("YYYY-MM-DD");
    },
    selectOK(rows, idstr) {
      let arr = [];
      if (this.peo == "producer") {
        this.form.producer = rows;
      } else if (this.peo == "planName") {
        this.form.plan = rows;
        this.form.planName = rows.name;
      } else if (this.peo == "manager") {
        this.form.manager = rows;
        this.form.managerName = rows.name;
      } else if (this.peo == "main") {
        this.form.main = rows;
        this.form.mainName = rows.name;
      }
      if (this.peo == "Craftsman") {
        this.form.Craftsman = rows;
        this.form.CraftsmanName = rows.name;
        // let obj = {
        //   name: this.userInfo.empname,
        //   uid: this.userInfo.empno,
        // };
        // arr.push(obj);
        // arr.push(rows);
        // let name = [];
        // let uid = [];
        // arr.forEach((item) => {
        //   name.push(item.name);
        // });
        // arr.forEach((item) => {
        //   uid.push(item.uid);
        // });
        // this.form.Craftsman = {
        //   name: JSON.stringify(name).replace(/\"|\[|\]/g, ""),
        //   uid: uid,
        // };
      }
    },
    // 根据工艺路线编码，版本，获取工艺路线
    getRoute(that) {
      new Promise((resolve) => {
        mesPeProcess
          .getRouter({ routerCode: this.routerCode, version: this.version })
          .then((res) => {
            if (res.code === 0) {
              resolve(res.data);
            } else {
              this.$message.warning(res.msg);
              this.$message.error(res.msg);
            }
          });
      }).then((res) => {
        this.showFlag = true;
        this.pdata = res;
      });
    },

    //   加载数据字典
    lodacontent() {
      Promise.all([
        new Promise((resolve) => {
          ajaxGetDictItems("InventoryChanges").then((res) => {
            if (res.code == 0) {
              resolve(res.data);
            } else {
              this.$message.error(res.msg);
            }
          });
        }),
        new Promise((resolve) => {
          ajaxGetDictItems("processChange").then((res) => {
            if (res.code == 0) {
              resolve(res.data);
            } else {
              this.$message.error(res.msg);
            }
          });
        }),
        new Promise((resolve) => {
          ajaxGetDictItems("mes_change_Company").then((res) => {
            if (res.code == 0) {
              resolve(res.data);
            } else {
              this.$message.error(res.msg);
            }
          });
        }),
        new Promise((resolve) => {
          ajaxGetDictItems("InventoryPackaging").then((res) => {
            if (res.code == 0) {
              resolve(res.data);
            } else {
              this.$message.error(res.msg);
            }
          });
        }),
      ]).then((res) => {
        this.list = res[0];
        this.processChange = res[1];
        this.Company = res[2];
        this.InventoryPackaging = res[3];
        this.queryList();
      });

      // ajaxGetDictItems("InventoryPackaging").then((res) => {
      //   if (res.code == 0) {
      //     this.InventoryPackaging = res.data;
      //   } else {
      //     this.$message.error(res.msg);
      //   }
      // });
      // ajaxGetDictItems("processChange").then((res) => {
      //   if (res.code == 0) {
      //     this.processChange = res.data;
      //   } else {
      //     this.$message.error(res.msg);
      //   }
      // });
      // ajaxGetDictItems("mes_change_Company").then((res) => {
      //   if (res.code == 0) {
      //     this.Company = res.data;
      //   } else {
      //     this.$message.error(res.msg);
      //   }
      // });
    },
    //  获取工艺路线信息
    processHandleConfirm() {
      const that = this;
      // 基本信息校验
      let params = {
        mesPeRouteStationDTOS: [],
      };
      // 校验画布 并获取正确顺序的节点
      const drawRoute = that.$refs.DrawRoute;
      const curSortPens = drawRoute.getSortNodeArr();
      if (!curSortPens || curSortPens.length === 0) {
        return;
      }
      // 保存前端数据
      console.log(drawRoute.canvas.data.pens);
      params.frontData = JSON.stringify(drawRoute.canvas.data.pens);
      // 工艺路线基本信息数据

      // type=1 新建 type=2 修改
      params.type = this.handle === "edit" ? 2 : 1;

      let topIndex = 1; // 一级工序顺序 从 1开始
      // 工站数据
      console.log(curSortPens);
      curSortPens.forEach((element) => {
        if (element.data && element.data.key === "process") {
          // 当前节点配置的一级工序数据
          const topSite = element.data.firstItem;
          // 当前一级工序工时
          let topHourArr = [];
          element.data.configTree.forEach((elem) => {
            elem.children.forEach((el) => {
              topHourArr.push(el.siteAttr.workstationHour);
            });
          });
          let topHour = 0;
          if (topHourArr.length > 0) {
            topHour = topHourArr.reduce((total, num) => total + num);
          }
          // 配置的二级工序以及工站
          element.data.configTree.forEach((elem, index) => {
            // 当前二级工序工时
            let twoHour = 0;
            const twoHourArr = elem.children.map(
              (et) => et.siteAttr.workstationHour
            );
            if (twoHourArr.length > 0) {
              twoHour = twoHourArr.reduce((total, num) => total + num);
            }
            // 配置的工站
            elem.children.forEach((el, inD) => {
              console.log(el);
              const siteAttr = el.siteAttr;
              let returnWorkstation = ["", ""];
              let returnWorkstationCodeArr = siteAttr.returnWorkstationCode.split(
                "-"
              );
              if (returnWorkstationCodeArr.length > 0) {
                returnWorkstation = returnWorkstationCodeArr;
              }
              let picturesData = el.sopData.picturesData;
              if (
                Object.prototype.toString.call(el.sopData.picturesData) ===
                "[object String]"
              ) {
                picturesData = JSON.parse(picturesData);
              }
              params.mesPeRouteStationDTOS.push({
                // 工站基本属性
                topHour: topHour,
                topOrder: topIndex,
                topProcessName: siteAttr.topProcessName,
                twoHour: twoHour,
                twoOrder: index + 1,
                twoProcessName: siteAttr.twoProcessName,
                workAddressCode: siteAttr.workAddressCode,
                workstationCode: siteAttr.workstationCode,
                workstationHour: siteAttr.workstationHour,
                workstationName: siteAttr.workstationName,
                workstationOrder: inD + 1,
                returnWorkstationCode: returnWorkstation[0],
                returnWorkstationName: returnWorkstation[1],
                collectionBarcode: siteAttr.collectionBarcode, // 是否采集条码 1 是 2 否
                collectionType: siteAttr.collectionType, // 条码采集类型
                // 技能
                mesPeRouterSkillList: el.skillData,
                // 资源
                mesPeRouterResourceList: el.sourceData,
                // 工站BOM
                mesPeBomMaintenanceList: el.bomData,
                // 线材清单
                mesPeWireListingList: el.wirerodData,
                // 组件清单
                mesPeComponentListList: el.moduleData,
                // 耗损系数
                mesPeConsumeCoefficientList: el.consumeData,
                // 程序
                mesPeConsumeCoefficientList: el.gramData,
                // SOP -----
                // 图片
                makePictures: picturesData.map((el) => {
                  return {
                    pictureUrl: el.filePath,
                  };
                }),
                // 操作步骤
                mesPeMattersNeedAttentions: el.sopData.stepData,
              });
            });
          });
          topIndex++;
        }
      });
      this.temporary = params.mesPeBomMaintenanceList;
      return params;
    },
    // 获取接口所需参数
    getObj(data, listid) {
      return {
        billNum: this.form.number, //单据号
        billOriNum: this.form.source, //来源单号
        billCreate: this.form.producer.uid, //制单人
        billCreateName: this.form.producer.name, //制单人名称
        changeCompany: this.form.Company, //更改单位
        changeType: this.form.type.value.toString(), //更改类型
        productChangeExplain: this.form.explain, //更改来源说明
        changeMaterialSet: this.form.Complete.toString(), //是否齐套
        productCodeChangeBefore: this.form.beforeNumber, //更改前编码
        productCodeChangeAfter: this.form.afterNumber, //更改后编码
        planProcessor: this.form.plan.uid, //计划处理人
        planProcessorName: this.form.plan.name, //计划处理人名称
        processProcessor: this.form.Craftsman.uid, //工艺处理人
        packingMaterialRetention: this.form.Material.toString(), //包材保留
        productChangeContent: this.form.list.toString(), //产品更改内容
        processSupervisor: this.form.main.uid, //工艺主管工号
        processSupervisorName: this.form.mainName,
        processManager: this.form.manager.uid, //工艺主管经理
        processManagerName: this.form.managerName,
        processOrderList: [
          {
            mesPeCraftRouteReq: this.processHandleConfirm(),
          },
        ], //工艺路线内容
        billStatus: data,
        id: listid,
      };
    },
    // 审批通过函数
    confirm() {
      let statues = this.listData.billStatus;
      if (statues == 1) {
        if (
          this.screen(this.form.Craftsman.uid[1]) ||
          this.screen(this.form.producer.uid)
        ) {
          this.saveSubmit(2, "审批成功");
        } else {
          this.$message.error("暂无权限确认");
        }
      } else if (statues == 2) {
        if (this.screen(this.form.plan.uid)) {
          this.saveSubmit(4, "审批成功");
        } else {
          this.$message.error("暂无权限确认");
        }
      } else if (statues == 4) {
        if (this.screen(this.form.main.uid)) {
          this.saveSubmit(5, "审批成功");
        } else {
          this.$message.error("暂无权限确认");
        }
      } else if (statues == 5) {
        if (this.screen(this.form.manager.uid)) {
          this.saveSubmit(6, "审批成功");
        } else {
          this.$message.error("暂无权限确认");
        }
      }
    },
    // 驳回
    reject() {
      let statues = this.listData.billStatus;
      console.log(statues);
      if (statues == 1) {
        if (
          this.screen(this.form.Craftsman.uid[1]) ||
          this.screen(this.form.producer.uid)
        ) {
          this.saveSubmit(0, "驳回成功");
        } else {
          this.$message.error("暂无权限驳回");
        }
      } else if (statues == 2) {
        if (this.screen(this.form.plan.uid)) {
          this.saveSubmit(0, "驳回成功");
        } else {
          this.$message.error("暂无权限驳回");
        }
      } else if (statues == 4) {
        if (this.screen(this.form.main.uid)) {
          this.saveSubmit(0, "驳回成功");
        } else {
          this.$message.error("暂无权限驳回");
        }
      } else if (statues == 5) {
        if (this.screen(this.form.manager.uid)) {
          this.saveSubmit(0, "驳回成功");
        } else {
          this.$message.error("暂无权限驳回");
        }
      }
    },
    // 判断当前登录人工号是否为目标工号
    screen(data) {
      if (this.userInfo.empno == data) {
        return true;
      } else {
        return false;
      }
    },
    // 保存编辑函数
    saveSubmit(data, test) {
      this.btnLoading = true;
      putAction(this.url.save, this.getObj(data, this.listData.id)).then(
        (res) => {
          if (res.code == 0) {
            this.$message.success(test);
             this.btnLoading = false;
            this.$emit("closeretentionVisible");
          } else {
            this.$message.error(res.msg);
          }
        }
      );
    },
  },
};
</script>
<style  scoped>
.poros-form .poros-form-item {
  height: 44px;
}
</style>